{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "💥第四组职业并不具有随机性，因为收集到的数据就是以本科生和另一个为主"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from statsmodels.sandbox.stats.runs import runstest_1samp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = pd.read_excel('../原始数据/原始答卷数据.xlsx')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "Gender = data['1. 请问您的性别是?']\n",
    "Age = data['2.请问您的年龄是?']\n",
    "Degree = data['3.请问您的最高学历是?']\n",
    "Major = data['4.请问您的职业是?']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[np.float64(1.5085470085470085), np.float64(2.5256410256410255), np.float64(2.9572649572649574), np.float64(4.239316239316239)]\n"
     ]
    }
   ],
   "source": [
    "# 将年龄，最高学历和职业转换为平均值符号序列\n",
    "means = [np.mean(Gender),np.mean(Age), np.mean(Degree), np.mean(Major)]\n",
    "def transform_to_median(data):\n",
    "    mean = np.mean(data)\n",
    "    return data.apply(lambda x: 2 if x >= mean else 1)\n",
    "Groups = [Gender, transform_to_median(Age), transform_to_median(Degree), transform_to_median(Major)]\n",
    "print(means)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Group 1\n",
      "均值（Mean）：1.51\n",
      "小于均值的数据数量：115\n",
      "大于等于均值的数据数量：119\n",
      "数据总数（Sample Size）：234\n",
      "游程数（Runs）：111\n",
      "Z 统计量（Z-Statistic）：-0.9130\n",
      "双侧渐进显著性（p-value）：0.3613\n",
      "无法拒绝 H₀，数据可能是随机的\n",
      "\n",
      "Group 2\n",
      "均值（Mean）：2.53\n",
      "小于均值的数据数量：119\n",
      "大于等于均值的数据数量：115\n",
      "数据总数（Sample Size）：234\n",
      "游程数（Runs）：117\n",
      "Z 统计量（Z-Statistic）：-0.1266\n",
      "双侧渐进显著性（p-value）：0.8993\n",
      "无法拒绝 H₀，数据可能是随机的\n",
      "\n",
      "Group 3\n",
      "均值（Mean）：2.96\n",
      "小于均值的数据数量：46\n",
      "大于等于均值的数据数量：188\n",
      "数据总数（Sample Size）：234\n",
      "游程数（Runs）：77\n",
      "Z 统计量（Z-Statistic）：0.4336\n",
      "双侧渐进显著性（p-value）：0.6646\n",
      "无法拒绝 H₀，数据可能是随机的\n",
      "\n",
      "Group 4\n",
      "均值（Mean）：4.24\n",
      "小于均值的数据数量：104\n",
      "大于等于均值的数据数量：130\n",
      "数据总数（Sample Size）：234\n",
      "游程数（Runs）：91\n",
      "Z 统计量（Z-Statistic）：-3.3905\n",
      "双侧渐进显著性（p-value）：0.0007\n",
      "拒绝 H₀，数据可能具有某种模式\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 进行游程检验,同时输出每组数据的均值，小于均值的数据数量，大于等于均值的数据数量，数据总数，游程数，Z统计量，双侧渐进显著性\n",
    "for i in range(4):\n",
    "    print('Group', i + 1)\n",
    "    mean_value = means[i]\n",
    "    num_less_than_mean = np.sum(Groups[i] == 1)\n",
    "    num_greater_equal_mean = np.sum(Groups[i] == 2)\n",
    "    total_count = len(Groups[i])\n",
    "    binary_seq = np.where(Groups[i] == 2, 1, 0)\n",
    "    runs_stat, p_value = runstest_1samp(binary_seq)\n",
    "    runs = np.sum(np.diff(binary_seq) != 0) + 1\n",
    "\n",
    "    print(f\"均值（Mean）：{mean_value:.2f}\")\n",
    "    print(f\"小于均值的数据数量：{num_less_than_mean}\")\n",
    "    print(f\"大于等于均值的数据数量：{num_greater_equal_mean}\")\n",
    "    print(f\"数据总数（Sample Size）：{total_count}\")\n",
    "    print(f\"游程数（Runs）：{runs}\")\n",
    "    print(f\"Z 统计量（Z-Statistic）：{runs_stat:.4f}\")\n",
    "    print(f\"双侧渐进显著性（p-value）：{p_value:.4f}\")\n",
    "\n",
    "    if p_value > 0.05:\n",
    "        print(\"无法拒绝 H₀，数据可能是随机的\")\n",
    "    else:\n",
    "        print(\"拒绝 H₀，数据可能具有某种模式\")\n",
    "    print('')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
